/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.knolle.vistas;

import java.awt.Color;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.jdesktop.swingx.JXDatePicker;

public final class CampaniasVisualPanel1 extends JPanel implements DocumentListener{

    /**
     * Campos de Propiedades
     */
    public static final String PROPIEDAD_NOMBRE = "nombre";
    public static final String PROPIEDAD_FECHA_INICIO="fecha_inicio";
    public static final String PROPIEDAD_FECHA_FIN="fecha_fin";
    public static final String PROPIEDAD_DESCRIPCION="descripcion";
    public static final String PROPIEDAD_ESTA_ACTIVO="esta_activo";
    
    /**
     * Creates new form CampaniasVisualPanel1
     */
   
   public CampaniasVisualPanel1() {
        initComponents();
        addListener();
    }

    @Override
    public String getName() {
        return "Paso #1";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        campoNombre = new javax.swing.JTextField();
        checkEstaActivo = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        lblDescripcion = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoDescripcion = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        campoFechaInicio = new org.jdesktop.swingx.JXDatePicker();
        campoFechaFin = new org.jdesktop.swingx.JXDatePicker();

        campoNombre.setText(org.openide.util.NbBundle.getMessage(CampaniasVisualPanel1.class, "CampaniasVisualPanel1.campoNombre.text_1")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(checkEstaActivo, org.openide.util.NbBundle.getMessage(CampaniasVisualPanel1.class, "CampaniasVisualPanel1.checkEstaActivo.text_1")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(CampaniasVisualPanel1.class, "CampaniasVisualPanel1.jLabel1.text_1")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(lblDescripcion, org.openide.util.NbBundle.getMessage(CampaniasVisualPanel1.class, "CampaniasVisualPanel1.lblDescripcion.text_1")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(CampaniasVisualPanel1.class, "CampaniasVisualPanel1.jLabel3.text_1")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(CampaniasVisualPanel1.class, "CampaniasVisualPanel1.jLabel4.text_1")); // NOI18N

        campoDescripcion.setColumns(20);
        campoDescripcion.setRows(5);
        jScrollPane1.setViewportView(campoDescripcion);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(CampaniasVisualPanel1.class, "CampaniasVisualPanel1.jLabel5.text_1")); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText(org.openide.util.NbBundle.getMessage(CampaniasVisualPanel1.class, "CampaniasVisualPanel1.jTextArea1.text_1")); // NOI18N
        jScrollPane2.setViewportView(jTextArea1);
        jTextArea1.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(CampaniasVisualPanel1.class, "CampaniasVisualPanel1.jLabel6.text_1")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(lblDescripcion)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(campoNombre)
                            .addComponent(campoFechaInicio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(campoFechaFin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                            .addComponent(checkEstaActivo, javax.swing.GroupLayout.Alignment.LEADING))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(campoFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkEstaActivo)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcion)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea campoDescripcion;
    private org.jdesktop.swingx.JXDatePicker campoFechaFin;
    private org.jdesktop.swingx.JXDatePicker campoFechaInicio;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JCheckBox checkEstaActivo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel lblDescripcion;
    // End of variables declaration//GEN-END:variables
    /**
     * Metodos para acceder a los componentes cargados por el usuario
     */
    public JTextField getCampoNombre(){
        return this.campoNombre;    
    }
    
    public JXDatePicker getCampoFechaInicio(){
        return this.campoFechaInicio;
    }
    
    public JXDatePicker getCampoFechaFin(){
       return this.campoFechaFin;
    }
    
    public javax.swing.JCheckBox getCheckEstaActivo() {
       return this.checkEstaActivo;
    }
    
    public javax.swing.JTextArea getCampoDescripcion (){
        return this.campoDescripcion;
    }
    
    /** Permite validar el campo nombre si se encuetra errores se modifica los bordes
     * se agregan mensajes 
     * @return 
     */
    
    public Boolean validarNombre(){
        if (getCampoNombre().getText().equals("") || (getCampoNombre().getText().length() < 3) ){
           getCampoNombre().setBorder(bordeError);
           return false;
        }        
        return true;
    }
    
    /**
     * Valida de fecha de Inicio, debe permitir ingresar fechas no muy viejas, la fecha de inicio debe ser inferior a la fecha de fin
     * modifica los bordes si encuentra errores
     * @return true is esta correcto
     */
    public Boolean validarFechaInicio(){
        if (getCampoFechaInicio().getDate() == null ){
           getCampoFechaInicio().setBorder(bordeError);
           return false;
        }        
        return true;
    }
    
    /**
     * Valida la fecha de fin de la campaña, si la fecha de fin es menor a la fecha de inicio, error,
     * si la fecha fin esta mal error, si la fecha es muy lejana pensar que hacer
     * @return true or false
     */
    public Boolean validarFechaFin(){
        if (getCampoFechaFin().getDate() == null ){
           getCampoFechaFin().setBorder(bordeError);
           return false;
        }        
        return true;
    }
    
    private void addListener(){
//        for (int i = 0 ; i < this.getComponentCount(); i++){
//            Component c = this.getComponent(i);
//            if (c instanceof JTextField || c instanceof JXDatePicker || c instanceof JTextArea) {
//                
//            }
//            else if(c instanceof JFormattedTextField) {
//                c.addFocusListener(focusListener);
//            }
//        }
        campoNombre.getDocument().addDocumentListener(this);
        campoFechaInicio.getEditor().getDocument().addDocumentListener(this);
        campoFechaFin.getEditor().getDocument().addDocumentListener(this);
        campoDescripcion.getDocument().addDocumentListener(this);
        
    }
    
    
//    private FocusListener focusListener = new FocusListener() {
//
//        @Override
//        public void focusGained(FocusEvent e) {
//            JComponent c = (JComponent) e.getSource();
//            c.setBorder(bordeFoco);
//            
//        }
//
//        @Override
//        public void focusLost(FocusEvent e) {
//            JComponent c = (JComponent) e.getSource();
//            c.setBorder(bordeNormal);
//        }
//    };
    
       
    private Border bordeError = BorderFactory.createEtchedBorder(Color.RED, Color.red);
//    private Border bordeFoco = BorderFactory.createEtchedBorder(Color.lightGray, Color.yellow);
//    private Border bordeNormal = new JTextField().getBorder();
    
//    private Border bordeError(){
//        Border borde = new Border() {
//
//            @Override
//            public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
//                throw new UnsupportedOperationException("Not supported yet.");
//            }
//
//            @Override
//            public Insets getBorderInsets(Component c) {
//                throw new UnsupportedOperationException("Not supported yet.");
//            }
//
//            @Override
//            public boolean isBorderOpaque() {
//                throw new UnsupportedOperationException("Not supported yet.");
//            }
//        }
//    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        dispararEventos(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        dispararEventos(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        dispararEventos(e);
    }
    
    private void dispararEventos(DocumentEvent e) {
       if (campoNombre.getDocument() == e.getDocument()) {
           firePropertyChange(PROPIEDAD_NOMBRE, 0, 1);       
       }
       if (campoFechaInicio.getEditor().getDocument() == e.getDocument()) {
           firePropertyChange(PROPIEDAD_FECHA_INICIO, 0, 1);
       }
       if (campoFechaFin.getEditor().getDocument() == e.getDocument()) {
           firePropertyChange(PROPIEDAD_FECHA_FIN, 0, 1);
       }
       if (campoDescripcion.getDocument() == e.getDocument()) {
           firePropertyChange(PROPIEDAD_DESCRIPCION, 0, 1);
       }
       
    }
}
